package com.foreknow.dao.inpl;

import com.foreknow.dao.AbcDao;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Abc;
import com.foreknow.model.Admin;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AbcDaoImpl extends BaseDao implements AbcDao {

    //查询单个
    @Override
    public Abc isLogin(int id, String name) {
        String sql="select * from abc where id= ? and name=? ";
       EntityMapping mapping= mappingFactory.getMap("ABCMAPPINGIMPL");
       List<Object>list= jdbcTemplate.query(sql,mapping,id,name);
        //3. 返回结果
        if (list.size()>0){
            return (Abc) list.get(0);
        }else {
            return null;

        }
    }

    //查询所有
    @Override
    public List<Object> query() {
        String sql="select * from abc ";
        EntityMapping mapping= mappingFactory.getMap("ABCMAPPINGIMPL");
        List<Object>list= jdbcTemplate.query(sql,mapping);
        return list;
    }

    //修改
    @Override
    public boolean update(Abc abc) throws SQLException {
        String sql="update abc set name=? where id=?";
        int update= jdbcTemplate.update(sql,abc.getName(),abc.getId());
        return update==1;
    }

    //插入
    @Override
    public boolean add(Abc abc) throws SQLException {
        String sql="insert into abc (name,createtime) values (?,?)";
        int add= jdbcTemplate.update(sql,abc.getName(),abc.getCreatetime());
        return add==1;
    }

    //根据id删除
    @Override
    public boolean delete(int id) throws SQLException {
        String sql= "delete from abc where id=?";
        int delete =jdbcTemplate.update(sql,id);
        return delete==1;
    }
}
